package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.common.constant.Constant;
import com.qf.entity.dto.Goods;
import com.qf.entity.dto.GoodsSku;
import com.qf.entity.dto.Order;
import com.qf.entity.po.LoginMerchant;
import com.qf.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService extends ServiceImpl<GoodsMapper, Goods> {

    @Autowired
    private GoodsSkuService goodsSkuService;

    @Transactional(readOnly = true)
    public List<Goods> getByTypeId(Long goodsTypeId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (goodsTypeId > 0) {
            wrapper.lambda().eq(Goods::getGoodsTypeId, goodsTypeId);
        }
        wrapper.lambda().orderByAsc(Goods::getOrderNum);
        return this.list(wrapper);
    }

    @Transactional
    public void up(Long goodsId) {
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Goods::getId, goodsId).set(Goods::getState, Goods.State.UP.getCode());
        this.update(wrapper);
    }

    @Transactional
    public void down(Long goodsId) {
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Goods::getId, goodsId).set(Goods::getState, Goods.State.DOWN.getCode());
        this.update(wrapper);
    }

    public Object getByMerchantUserId(Long id) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantUserId, id);
        return this.list(wrapper);
    }

    @Transactional
    public void saveGoods(Goods goods, LoginMerchant loginMerchant) {
        //当传来的商品id不为空时，删除掉对应商品id的所有SKU
        if (goods.getId() != null) {
            goodsSkuService.removeByGoodsId(goods.getId());
        }
        //将登陆操作pp的id填充goods
        goods.setMerchantUserId(loginMerchant.getId());
        goods.setState(Goods.State.PENDING.getCode());
        //新增或修改操作
        this.saveOrUpdate(goods);

        String[] titles = goods.getSkuTitle().split(Constant.REGEX);
        String[] costs = goods.getSkuCost().split(Constant.REGEX);
        String[] prices = goods.getSkuPrice().split(Constant.REGEX);
        String[] pmoneys = goods.getSkuPmoney().split(Constant.REGEX);
        List<GoodsSku> needInsert = new ArrayList<>(titles.length);
        for (int i = 0; i < titles.length ; i++) {
            GoodsSku goodsSku = new GoodsSku();
            goodsSku.setGoodId(goods.getId());
            goodsSku.setTitle(titles[i]);
            goodsSku.setCost(costs[i]);
            goodsSku.setPrice(prices[i]);
            goodsSku.setPmoney(pmoneys[i]);
            goodsSku.setOrderNo(new Long(i+1));
            needInsert.add(goodsSku);
        }
        goodsSkuService.saveBatch(needInsert,needInsert.size());
    }

    public List<Goods> getUpGoodsByMerchangUserId(LoginMerchant loginMerchant) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantUserId,loginMerchant.getId())
        .eq(Goods::getState,Goods.State.UP.getCode());
        return this.list(wrapper);
    }
}